package jp.sourceforge.akjrcp.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;

public class GenericParametersTableViewer extends TableViewer{
	private List parameterList;
	
	public GenericParameters getCurrentSelection(){
		int selection=this.getTable().getSelectionIndex();
		if(selection>-1){
		return (GenericParameters) this.getGenericParameters().get(selection);
		}else{
			return null;
		}
		
		
	}
	public GenericParametersTableViewer(Composite parent) {
		super(parent);
		List list = new ArrayList();
		
		this.setLabelProvider(new ViewLabelProvider());
		this.setContentProvider(new ViewContentProvider());
		setGenericParametersList(list);
	}
	
	public static List loadList(String value){
		List list=new ArrayList();
		GenericParameters[] settings = GenericParametersTableViewer.toGenericParameters(value);
		for (int i = 0; i < settings.length; i++) {
			list.add(settings[i]);
		}
		return list;
		//refresh();
	}

	//print label
	public String toLabel(GenericParameters setting){
		
		String label="";
		label+=setting.getName()+" - "+StringUtils.replace(setting.getComment(),"\n"," ");
		return label;
	}
	
	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {

			return toLabel((GenericParameters) obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return null;
		}

	}
	
	public static GenericParameters[] toGenericParameters(String lines) {
		if (lines.length() == 0) {
			return new GenericParameters[0];
		}
		lines=StringUtils.replace(lines,"\r\n","\n");
		String line[] = lines.split("\n");
		List<GenericParameters> tmpList=new ArrayList<GenericParameters>();
		
		for (int i = 0; i < line.length; i++) {
			if(!line[i].equals("")){
			tmpList.add(toGenericParameter(line[i]));
			if(line[i].length()<10){
			//	log.info("'"+line[i]+"'"+line[i].length());
			}
			}
		}
		return tmpList.toArray(new GenericParameters[tmpList.size()]);
	}
	

	private static GenericParameters toGenericParameter(String text) {
		return GenericParameters.csvToParameters(text);
	}
	

	public static String toStoreString(GenericParameters setting) {

		return GenericParameters.parametersToCsv(setting);
	}


	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {

		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {

			return ((List) parent).toArray();
		}
	}

	public List getGenericParameters() {
		return parameterList;
	}

	public void setGenericParametersList(List parameterList) {
		this.parameterList = parameterList;
		this.setInput(parameterList);
		//this.refresh();
	}
	
	public String copiedName(GenericParameters parameter){
		Map nameMap=new HashMap();
		for(int i=0;i<parameterList.size();i++){
			GenericParameters p=(GenericParameters)parameterList.get(i);
			nameMap.put(p.getName(), "");
		}
		int index=1;
		String newName=null;
		String baseName=parameter.getName();
		Pattern numberPattern=Pattern.compile("^(.*)\\((\\d+)\\)$");
		Matcher matcher=numberPattern.matcher(baseName);
		if(matcher.matches()){
			baseName=matcher.group(1);
			index=Integer.parseInt(matcher.group(2));
		}
		//TODO check all names
		while(true){
			newName=baseName+"("+index+")";
			if(nameMap.get(newName)==null){
				break;
			}
			index++;
		}
		return newName;
	}
}
